/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lotus.tep.event;

import com.lotus.tep.trouble.VoiceUiException;
import com.lotus.tep.ui.VoiceUi;

/**
 *
 * @author arvind.sharma
 */
public class VoiceUiHandler {

	private final VoiceUi voiceUi_;
	private String voiceText_;

	private VoiceUiHandler(VoiceUi voiceUi) {
		voiceUi_ = voiceUi;
		voiceText_ = "";
	}

	public static VoiceUiHandler createVoiceControlHandler(VoiceUi voiceUi) throws VoiceUiException {
		if (null == voiceUi) {
			throw VoiceUiException.createNullVoiceUiException();
		}
		VoiceUiHandler vch = new VoiceUiHandler(voiceUi);
		new Thread(vch.new VoiceMonitorThread()).start();
		return vch;
	}

	private String getVoiceText() {
		return voiceText_;
	}

	public void setVoiceText(String voiceText) {
		voiceText_ = null == voiceText ? voiceText_ : voiceText;
	}

	public void clearVoiceText() {
		voiceText_ = "";
	}

	private class VoiceMonitorThread implements Runnable {

		@Override
		public void run() {
			while (true) {
				boolean enbl = !getVoiceText().trim().isEmpty();
				voiceUi_.setEnabledPlay(enbl);
				voiceUi_.setEnabledPause(enbl);
				voiceUi_.setEnabledResume(enbl);
				voiceUi_.setEnabledStop(enbl);
			}
		}
	}
}
